# 9. 回文数

var isPalindrome = function(x) {
  if (x < 0) return false;
  flag = true;

  const str = x + "";

  for (let i = str.length - 1; i > 0; i--) {
    if (str[i] !== str[str.length - 1 - i]) {
      flag = false;
      break;
    }
  }

  return flag;
};

console.log(isPalindrome(121));
console.log(isPalindrome(-121));
console.log(isPalindrome(10));
console.log(isPalindrome(10101));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Last Updated: 6/27/2023, 7:40:45 PM